package com.wyp168.es.controller;

import com.wyp168.common.exception.BizCodeException;
import com.wyp168.common.to.es.SkuESModel;
import com.wyp168.common.utils.R;
import com.wyp168.es.service.ESService;
import com.wyp168.es.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ESController
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2023/8/12 14:29
 */
@Slf4j
@RestController
@RequestMapping("/search")
public class ESController {

    @Autowired
    ESService esService;

    @Autowired
    ProductSaveService productSaveService;

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public R search() {
        esService.list();
        return R.ok();
    }

    @PostMapping(value = "/product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModelList) {
        try {
            Boolean isSyncSuccess = productSaveService.productStatusUp(skuESModelList);
            if (isSyncSuccess) {
                log.info("sku数据同步到es成功。");
            } else {
                log.error("sku数据同步到es失败。");
            }
            return R.ok();
        } catch (Exception e) {
            log.info("sku数据同步到es失败，e:{}", e.getMessage());
            return R.error(BizCodeException.PRODUCT_UP_EXCEPTION.getCode(), BizCodeException.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }

}
